﻿using System.Threading;

namespace Common.Contexts
{
    /// <summary>
    /// 用户上下文
    /// </summary>
    public class UserContext
    {
        private static AsyncLocal<long?> _userId = new AsyncLocal<long?>();
        private static AsyncLocal<string> _userName = new AsyncLocal<string>();

        /// <summary>
        /// 获取当前用户id
        /// </summary>
        public static long? UserId
        {
            get => _userId.Value;
            set => _userId.Value = value;
        }

        /// <summary>
        /// 获取当前用户名
        /// </summary>
        public static string UserName
        {
            get => _userName.Value;
            set => _userName.Value = value;
        }

        /// <summary>
        /// 清空上下文
        /// </summary>
        public static void Clear()
        {
            _userId.Value = null;
            _userName.Value = null;
        }
    }
}